package com.sduept.nwld.dataserver.controller.config;

import com.sduept.bigdata.fault.entity.ProcessDefinition;
import com.sduept.bigdata.fault.entity.ProcessItem;
import com.sduept.bigdata.fault.manager.ProcessItemConfigManager;
import com.sduept.bigdata.web.jsf.controller.AbstractController;
import com.sduept.generator.ObjectId;
import com.sduept.nwld.dataserver.util.LibTreeNode;
import com.sduept.utils.PingYinUtil;
import org.primefaces.event.NodeSelectEvent;
import org.primefaces.event.ReorderEvent;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.TreeNode;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import javax.inject.Named;
import javax.faces.view.ViewScoped;
import java.util.ArrayList;
import java.util.List;

@Named
@ViewScoped
public class ProcessItemConfigController extends AbstractController {
	private static final long serialVersionUID = -8302832229520764L;
	@Autowired
	private ProcessItemConfigManager manager;

	private TreeNode root;
	private TreeNode currentSelect;
	private List<ProcessDefinition> definitions = new ArrayList<ProcessDefinition>();
	private ProcessDefinition createDefinition = new ProcessDefinition();
	private ProcessDefinition selectDefinition = null;
	private List<ProcessItem> processItems = new ArrayList<ProcessItem>();
	private ProcessItem createItem = new ProcessItem();
	private ProcessItem editItem = null;
	private ProcessItem selected = null;

	@PostConstruct
	private void init() {
		root = new DefaultTreeNode();
		definitions = manager.findAllDefinition();
		initTree();
	}

	/**
	 * 重新加载显示树
	 */
	private void initTree() {
		root.getChildren().clear();
		if (definitions != null && definitions.size() != 0) {
			for (ProcessDefinition data : definitions) {
				new LibTreeNode("root", data, root);
			}
		}
	}

	/**
	 * 树节点编辑前操作
	 */
	public void preCreatDefinition() {
		createDefinition = new ProcessDefinition();
	}

	public void saveNewDefinition() {
		String code = PingYinUtil.getPingYinHeader(createDefinition.getName());
		ProcessDefinition exit = manager.findDefinitionById(code);
		if (exit == null) {
			createDefinition.setId(code);
			manager.saveDefinition(createDefinition);
			definitions.add(createDefinition);
			selectDefinition = null;
			currentSelect = null;
			initTree();
			addSuccessMessage("添加成功！");
		} else {
			addErrorMessage("名称【 " + createDefinition.getName() + " 】已被占用，请更改名称！");
		}
	}

	public void saveEditDefinition() {
		manager.saveDefinition(selectDefinition);
		selectDefinition = null;
		currentSelect = null;
		initTree();
		addSuccessMessage("修改成功！");
	}

	public void deleteDefinition() {
		definitions.remove(selectDefinition);
		processItems.clear();
		manager.deleteProcessItemsByPid(selectDefinition.getId());
		manager.deleteDefinition(selectDefinition);
		selectDefinition = null;
		currentSelect = null;
		initTree();
		addSuccessMessage("删除成功！");
	}

	public void onNodeSelect(NodeSelectEvent event) {
		currentSelect = event.getTreeNode();
		selectDefinition = (ProcessDefinition) currentSelect.getData();
		processItems = manager.findProcessItemsByPid(selectDefinition.getId());
	}

	public void onRowSelect(SelectEvent event) {
		selected = (ProcessItem) event.getObject();
	}

	public void preCreateItem() {
		createItem = new ProcessItem();
		createItem.setId(ObjectId.get().toString());
		createItem.setPid(selectDefinition != null ? selectDefinition.getId() : "");
	}

	public void saveNewItem() {
		processItems.add(0, createItem);
		manager.saveItem(createItem);
		selected = null;
		addSuccessMessage("添加成功！");
	}

	public void saveEditItem() {
		editItem = selected;
		manager.saveItem(editItem);
		selected = null;
		addSuccessMessage("修改成功！");
	}

	public void deleteItem() {
		if (selected != null) {
			processItems.remove(selected);
			manager.deleteProcessItem(selected);
			selected = null;
			addSuccessMessage("删除成功！");
		}
	}

	/**
	 * 重新排列
	 *
	 * @param event
	 */
	public void onRowReorder(ReorderEvent event) {
		int fromIndex = event.getFromIndex();
		int toIndex = event.getToIndex();
		if (toIndex != fromIndex) {
			if (toIndex < fromIndex) {
				for (int i = toIndex + 1; i <= fromIndex; i++) {
					ProcessItem changeItem = processItems.get(i);
					changeItem.setOrderNo(changeItem.getOrderNo() + 1);
					manager.saveItem(changeItem);
				}
			} else if (toIndex > fromIndex) {
				for (int i = fromIndex; i <= toIndex; i++) {
					ProcessItem changeItem = processItems.get(i);
					changeItem.setOrderNo(changeItem.getOrderNo() - 1);
					manager.saveItem(changeItem);
				}
			}
			ProcessItem sel = processItems.get(toIndex);
			sel.setOrderNo(toIndex + 1);
			manager.saveItem(sel);
			processItems = manager.findProcessItemsByPid(selectDefinition.getId());
		}
		addSuccessMessage("排序成功！");
	}

	public List<ProcessDefinition> getDefinitions() {
		return definitions;
	}

	public void setDefinitions(List<ProcessDefinition> definitions) {
		this.definitions = definitions;
	}

	public ProcessDefinition getCreateDefinition() {
		return createDefinition;
	}

	public void setCreateDefinition(ProcessDefinition createDefinition) {
		this.createDefinition = createDefinition;
	}

	public ProcessDefinition getSelectDefinition() {
		return selectDefinition;
	}

	public void setSelectDefinition(ProcessDefinition selectDefinition) {
		this.selectDefinition = selectDefinition;
	}

	public List<ProcessItem> getProcessItems() {
		return processItems;
	}

	public void setProcessItems(List<ProcessItem> processItems) {
		this.processItems = processItems;
	}

	public ProcessItem getCreateItem() {
		return createItem;
	}

	public void setCreateItem(ProcessItem createItem) {
		this.createItem = createItem;
	}

	public ProcessItem getEditItem() {
		return editItem;
	}

	public void setEditItem(ProcessItem editItem) {
		this.editItem = editItem;
	}

	public TreeNode getRoot() {
		return root;
	}

	public void setRoot(TreeNode root) {
		this.root = root;
	}

	public TreeNode getCurrentSelect() {
		return currentSelect;
	}

	public void setCurrentSelect(TreeNode currentSelect) {
		this.currentSelect = currentSelect;
	}

	public ProcessItem getSelected() {
		return selected;
	}

	public void setSelected(ProcessItem selected) {
		this.selected = selected;
	}

}
